package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    //查询所有
    /*@RequestMapping("/list")
    public String list(Model model) {
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments", departments);
        return "department/list";
    }*/

    //带分页查询
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){
        PageResult<Department> pageResult = departmentService.query(qo);
        model.addAttribute("pageResult",pageResult);
        return "department/list";
    }

    //删除
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            departmentService.delete(id);
        }
        return "redirect:/department/list";
    }

    //新增 修改是同一个页面
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {//表示修改
            Department department = departmentService.get(id);
            model.addAttribute("department", department);
        }
        //新增
        return "department/input";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Department department){
        if(department.getId()==null){
            //新增
            departmentService.save(department);
        }else{
            //修改
            departmentService.update(department);
        }
        return "redirect:/department/list";
    }
}
